
function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
function createList(arr) {
  if(arr.length == 0)
    return null;
  let head = new ListNode(arr[0], null), headNode = head;
  for(let i = 1; i < arr.length; i++) {
    let temp = new ListNode(arr[i], null);
    head.next = temp;
    head = temp;
  }
  return headNode;
}
function showList(head) {
  while(head) {
    console.log(head.val);
    head = head.next;
  }
}
var oddEvenList = function(head) {
  if(head == null || head.next == null)
    return head;
  // head = createList(head);
  // showList(head);
  let count = 2, temp = head, result = head;
  let oddNode = temp, evenNode = null, evenHead = null;
  temp = temp.next;
  while(temp) {
    if(count % 2 == 1) {
      oddNode.next = temp;
      oddNode = oddNode.next;
    } else {
      if(!evenNode) {
        evenHead = temp;
        evenNode = temp;
      } else {
        evenNode.next = temp;
        evenNode = evenNode.next;
      }
    }
    temp = temp.next;
    count++;
  }
  evenNode.next = null;
  oddNode.next = null;
  oddNode.next = evenHead;
  return result;
};
let head = [1,2,3,4,5];
console.log(oddEvenList(head));
head = [2,1,3,5,6,4,7];
console.log(oddEvenList(head));
